using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class GameMap : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;

        protected Texture2D backgroundSprite;
        public Texture2D BackgroundSprite
        {
            get { return this.backgroundSprite; }
        }
        protected Rectangle backgroundSourceRectangle;
        public Rectangle BackgroundSourceRectangle
        {
            get { return this.backgroundSourceRectangle; }
        }
        protected String mapName;
        public String MapName
        {
            get { return this.mapName; }
        }
        protected String displayName;
        public String DisplayName
        {
            get { return this.displayName; }
        }
        protected Rectangle worldBounds;
        public Rectangle WorldBounds
        {
            get { return this.worldBounds; }
        }
        protected int perimeter;
        public int Perimeter
        {
            get { return perimeter; }
        }
        
        public GameMap(Game game, String mapName)
            : base(game)
        {
            this.mapName = mapName;
            // TODO: check if it's a valid map

            Game.Components.Add(this);

            // make sure it loads and draws
            DrawOrder = 0;
            UpdateOrder = 95;
            Visible = true;
            Enabled = false;

            //Initialize();
            LoadContent();
        }

        protected override void LoadContent()
        {
            // Setup a SpriteBatch for this set of rendering
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            // Load the map
            this.backgroundSprite = Game.Content.Load<Texture2D>(mapName + "\\Sprites\\map");
            this.backgroundSourceRectangle = new Rectangle(0, 0, 1024, 768);
            this.displayName = this.mapName; //TODO: change this to read a property

            // this may change when porting to different places, though maybe the map should be the same
            this.worldBounds = new Rectangle(0, 0, 1024, 768);
            this.perimeter = worldBounds.Width * 2 + worldBounds.Height * 2;
            
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            spriteBatch.Begin(SpriteBlendMode.None);
            spriteBatch.Draw(
                BackgroundSprite,
                Vector2.Zero,
                BackgroundSourceRectangle,
                Color.White);
            spriteBatch.End();

            // draw the TEMP killzone
            /*BasicEffect basicEffect = new BasicEffect(GraphicsDevice, null);
            basicEffect.Alpha = 1.0f;
            basicEffect.VertexColorEnabled = true;

            VertexPositionColor[] pointList = new VertexPositionColor[4];
            pointList[0] = new VertexPositionColor(new Vector3(WorldBounds.X + WorldBounds.Width / 2 - 150, WorldBounds.Y + WorldBounds.Height / 2 - 75, 0), Color.Red);
            pointList[1] = new VertexPositionColor(new Vector3(WorldBounds.X + WorldBounds.Width / 2 + 150, WorldBounds.Y + WorldBounds.Height / 2 - 75, 0), Color.Red);
            pointList[2] = new VertexPositionColor(new Vector3(WorldBounds.X + WorldBounds.Width / 2 + 150, WorldBounds.Y + WorldBounds.Height / 2 + 75, 0), Color.Red);
            pointList[3] = new VertexPositionColor(new Vector3(WorldBounds.X + WorldBounds.Width / 2 - 150, WorldBounds.Y + WorldBounds.Height / 2 + 75, 0), Color.Red);
            
            short[] lineStripIndices = new short[5] { 0, 1, 2, 3, 0 };
            basicEffect.Begin();
            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
                PrimitiveType.LineStrip,
                pointList,
                0,
                4,
                lineStripIndices,
                0,
                4);
            basicEffect.End();*/
        }
    }
}